/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HomeView.java
 *
 * Created on Apr 29, 2010, 3:00:19 PM
 */
package ticketmaster.presentation;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.security.cert.CertificateException;
import javax.swing.JOptionPane;
import ticketmaster.businesslogic.LoginController;
import ticketmaster.businessobject.LoginUser;
import ticketmaster.businessobject.User;
import ticketmaster.common.UserRoles;
import ticketmaster.presentation.administrator.AdministratorHomeView;
import ticketmaster.presentation.clerk.ClerkHomeView;
import ticketmaster.presentation.clerk.ClerkHomeViewPanel;
import ticketmaster.presentation.customer.CustomerHomeView;
import ticketmaster.presentation.customer.CustomerHomeViewPanel;
import ticketmaster.presentation.customer.purchase.SearchView;
import ticketmaster.util.StringHelper;

/**
 *
 * @author Wei Liu
 */
public class HomeView extends BaseFrame
{

    /** Creates new form HomeView */
    public HomeView()
    {
        initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlBody = new javax.swing.JPanel();
        pnlWelcome = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        pnlLogin = new javax.swing.JPanel();
        tfPassword = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();
        lbUsername = new javax.swing.JLabel();
        lbPassword = new javax.swing.JLabel();
        tfUsername = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        btnRegister = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        pnlTitle = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ticketmaster.TicketMasterApp.class).getContext().getResourceMap(HomeView.class);
        pnlBody.setBackground(resourceMap.getColor("pnlBody.background")); // NOI18N
        pnlBody.setName("pnlBody"); // NOI18N

        pnlWelcome.setBackground(resourceMap.getColor("pnlWelcome.background")); // NOI18N
        pnlWelcome.setName("pnlWelcome"); // NOI18N
        pnlWelcome.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setForeground(resourceMap.getColor("jLabel2.foreground")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        pnlWelcome.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 50, 180, 27));

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        pnlWelcome.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 0, 398, 40));

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        pnlWelcome.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 90, 510, 440));

        pnlLogin.setBackground(resourceMap.getColor("pnlLogin.background")); // NOI18N
        pnlLogin.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(resourceMap.getColor("pnlLogin.border.border.lineColor"), 1, true), resourceMap.getString("pnlLogin.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("pnlLogin.border.titleFont"), resourceMap.getColor("pnlLogin.border.titleColor"))); // NOI18N
        pnlLogin.setFont(resourceMap.getFont("pnlLogin.font")); // NOI18N
        pnlLogin.setName("pnlLogin"); // NOI18N
        pnlLogin.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        tfPassword.setFont(resourceMap.getFont("tfPassword.font")); // NOI18N
        tfPassword.setName("tfPassword"); // NOI18N
        pnlLogin.add(tfPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 140, 290, 32));

        btnLogin.setFont(resourceMap.getFont("btnLogin.font")); // NOI18N
        btnLogin.setText(resourceMap.getString("btnLogin.text")); // NOI18N
        btnLogin.setName("btnLogin"); // NOI18N
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });
        pnlLogin.add(btnLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 202, 290, 40));

        lbUsername.setFont(resourceMap.getFont("lbPassword.font")); // NOI18N
        lbUsername.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbUsername.setText(resourceMap.getString("lbUsername.text")); // NOI18N
        lbUsername.setName("lbUsername"); // NOI18N
        pnlLogin.add(lbUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 80, 90, 30));

        lbPassword.setFont(resourceMap.getFont("lbPassword.font")); // NOI18N
        lbPassword.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbPassword.setText(resourceMap.getString("lbPassword.text")); // NOI18N
        lbPassword.setName("lbPassword"); // NOI18N
        pnlLogin.add(lbPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 140, 90, 30));

        tfUsername.setFont(resourceMap.getFont("tfUsername.font")); // NOI18N
        tfUsername.setName("tfUsername"); // NOI18N
        pnlLogin.add(tfUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 80, 290, 32));

        jPanel2.setBackground(resourceMap.getColor("jPanel2.background")); // NOI18N
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(resourceMap.getColor("jPanel2.border.border.lineColor")), resourceMap.getString("jPanel2.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel2.border.titleFont"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N
        jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 50, 176, -1));

        btnRegister.setFont(resourceMap.getFont("btnRegister.font")); // NOI18N
        btnRegister.setText(resourceMap.getString("btnRegister.text")); // NOI18N
        btnRegister.setName("btnRegister"); // NOI18N
        btnRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegisterActionPerformed(evt);
            }
        });
        jPanel2.add(btnRegister, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 90, 133, 38));

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N
        jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 80, 270, -1));

        javax.swing.GroupLayout pnlBodyLayout = new javax.swing.GroupLayout(pnlBody);
        pnlBody.setLayout(pnlBodyLayout);
        pnlBodyLayout.setHorizontalGroup(
            pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBodyLayout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(pnlWelcome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addGroup(pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 550, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnlLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 550, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        pnlBodyLayout.setVerticalGroup(
            pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBodyLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(pnlBodyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlBodyLayout.createSequentialGroup()
                        .addComponent(pnlLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pnlWelcome, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(107, Short.MAX_VALUE))
        );

        getContentPane().add(pnlBody, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 130, 1200, 630));

        pnlTitle.setBackground(resourceMap.getColor("pnlTitle.background")); // NOI18N
        pnlTitle.setName("pnlTitle"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel7.setIcon(resourceMap.getIcon("jLabel7.icon")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        javax.swing.GroupLayout pnlTitleLayout = new javax.swing.GroupLayout(pnlTitle);
        pnlTitle.setLayout(pnlTitleLayout);
        pnlTitleLayout.setHorizontalGroup(
            pnlTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlTitleLayout.createSequentialGroup()
                .addGroup(pnlTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 1198, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlTitleLayout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlTitleLayout.setVerticalGroup(
            pnlTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlTitleLayout.createSequentialGroup()
                .addGroup(pnlTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlTitleLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE))
                    .addGroup(pnlTitleLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        getContentPane().add(pnlTitle, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1200, 130));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnLoginActionPerformed
    {//GEN-HEADEREND:event_btnLoginActionPerformed
        LoginController controller = new LoginController();
        String username = this.tfUsername.getText();
        String password = StringHelper.CharArrayToString(this.tfPassword.getPassword());

        User user = null;

        try
        {
            user = controller.validateUser(username, password);
        }
        catch (CertificateException ex)
        {
            JOptionPane.showMessageDialog(rootPane, ex.getMessage());
            return;
        }

        LoginUser.setUser(user);

        if (user.getRole() == UserRoles.Customer)
        {
            //CustomerHomeView customerHomeView = new CustomerHomeView();
            CustomerHomeViewPanel customerHomeView = new CustomerHomeViewPanel();
//            SearchView sv = new SearchView();
//            sv.setPnlParent(pnlBody);
            this.pnlBody.removeAll();
            this.pnlBody.add(customerHomeView);
            this.pnlBody.validate();
            this.pnlBody.repaint();
            //customerHomeView.setVisible(true);
        }
        else if(user.getRole() == UserRoles.Clerk)
        {
            ClerkHomeViewPanel clerkHomeView = new ClerkHomeViewPanel();
            this.pnlBody.removeAll();
            this.pnlBody.add(clerkHomeView);
            this.pnlBody.validate();
            this.pnlBody.repaint();
        }
        else if(user.getRole() == UserRoles.Administrator)
        {
            AdministratorHomeView administratorHomeView = new AdministratorHomeView();
            administratorHomeView.setVisible(true);
            this.setVisible(false);
        }
        //this.setVisible(false);
}//GEN-LAST:event_btnLoginActionPerformed

    private void btnRegisterActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnRegisterActionPerformed
    {//GEN-HEADEREND:event_btnRegisterActionPerformed
        // TODO add your handling code here:
        RegisterView rv = new RegisterView();
        rv.setVisible(true);
        this.setVisible(false);
        //        JFrame frame = new JFrame();
        //        CustomerRegisterPanel crp = new CustomerRegisterPanel();
        //        frame.setSize(750, 750);
        //        frame.add(crp);
        //        frame.setVisible(true);
    }//GEN-LAST:event_btnRegisterActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {

            public void run()
            {
                new HomeView().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnRegister;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lbPassword;
    private javax.swing.JLabel lbUsername;
    private javax.swing.JPanel pnlBody;
    private javax.swing.JPanel pnlLogin;
    private javax.swing.JPanel pnlTitle;
    private javax.swing.JPanel pnlWelcome;
    private javax.swing.JPasswordField tfPassword;
    private javax.swing.JTextField tfUsername;
    // End of variables declaration//GEN-END:variables

}
